package core;


import java.util.ArrayList;


class Palette {
	public int width, height; // in pixels
	public int x0, y0; // in pixels
	public static final float alpha = 0.3f; // if between 0 and 1, the palette is drawn semi-transparent

	public ArrayList< PaletteButton > buttons = null;

	// These variables are initialized in the contructor,
	// to save the index of each button,
	// but after that they should never change.
	public int movePalette_buttonIndex;
	public int ink_buttonIndex;
	public int select_buttonIndex;
	public int manipulate_buttonIndex;
	public int camera_buttonIndex;
	public int black_buttonIndex;
	public int red_buttonIndex;
	public int green_buttonIndex;
	public int horizFlip_buttonIndex;
	public int verticalFlip_buttonIndex;
	public int frameAll_buttonIndex;
	public int undo_buttonIndex;
	public int redo_buttonIndex;
	public int deleteSelection_buttonIndex;


	public int currentlyActiveModalButton; // could be equal to any of ink_buttonIndex, select_buttonIndex, manipulate_buttonIndex, camera_buttonIndex

	public int currentlyActiveColorButton; // could be equal to any of black_buttonIndex, red_buttonIndex, green_buttonIndex
	public float current_red = 0;
	public float current_green = 0;
	public float current_blue = 0;

	public Palette() {
		final int W = PaletteButton.width;
		final int H = PaletteButton.height;
		PaletteButton b = null;
		buttons = new ArrayList< PaletteButton >();


		// Create first row of buttons

		b = new PaletteButton(   0, 0, "Move", "Drag on this button to move the palette.", false );
		movePalette_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton(   W, 0, "Ink", "When active, use other fingers to draw ink strokes.", true );
		ink_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 2*W, 0, "Select", "When active, use another finger to select strokes.", true );
		select_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 3*W, 0, "Manip.", "When active, use one or two other fingers to directly manipulate the selection.", true );
		manipulate_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 4*W, 0, "Camera", "When active, use one or two other fingers to directly manipulate the camera.", true );
		camera_buttonIndex = buttons.size();
		buttons.add( b );


		// Create second row of buttons

		b = new PaletteButton(   0, H, "Black", "Changes ink color.", true );
		black_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton(   W, H, "Red", "Changes ink color.", true );
		red_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 2*W, H, "Green", "Changes ink color.", true );
		green_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 3*W, H, "Hor. Flip", "Flip the selection horizontally (around a vertical axis).", false );
		horizFlip_buttonIndex = buttons.size();
		buttons.add( b );
		
		b = new PaletteButton( 4*W, H, "Vert. Flip", "Flip the selection vertically (around a horizontal axis).", false );
		verticalFlip_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 0, 2*H, "Frame all", "Frames the entire drawing.", false );
		frameAll_buttonIndex = buttons.size();
		buttons.add( b );
		
		b = new PaletteButton( W, 2*H, "Undo", "Undoes the last action.", false );
		undo_buttonIndex = buttons.size();
		buttons.add( b );
		
		b = new PaletteButton( 2*W, 2*H, "Redo", "blabla.", false );
		redo_buttonIndex = buttons.size();
		buttons.add( b );

		b = new PaletteButton( 3*W, 2*H, "Delete", "Erases the current selection", false );
		deleteSelection_buttonIndex = buttons.size();
		buttons.add( b );

		// Initialize remaining state

		buttons.get( ink_buttonIndex ).isPressed = true;
		currentlyActiveModalButton = ink_buttonIndex;
		buttons.get( black_buttonIndex ).isPressed = true;
		currentlyActiveColorButton = black_buttonIndex;
		current_red = current_green = current_blue = 0;

		// Figure out the width and height of the palette.
		// To do this, compute a bounding rectangle.
		AlignedRectangle2D boundingRectangle = new AlignedRectangle2D();
		for ( int j = 0; j < buttons.size(); ++j ) {
			boundingRectangle.bound( buttons.get(j).getBoundingRectangle() );
		}
		// Note that the bounding rectangle contains coordinates in the palette's local space.
		// We only store the width and height of the bounding rectangle.
		width = Math.round( boundingRectangle.getDiagonal().x() );
		height = Math.round( boundingRectangle.getDiagonal().y() );
	}
	public AlignedRectangle2D getBoundingRectangle() {
		return new AlignedRectangle2D( new Point2D(x0,y0), new Point2D(x0+width,y0+height) );
	}
	public Point2D getCenter() {
		return getBoundingRectangle().getCenter();
	}
	public boolean contains( float x, float y ) {
		return getBoundingRectangle().contains( new Point2D(x,y) );
	}
	// returns -1 if no button contains the given point
	public int indexOfButtonContainingTheGivenPoint( float x, float y ) {
		for ( int j = 0; j < buttons.size(); ++j ) {
			PaletteButton b = buttons.get( j );
			if ( b.contains( x-x0, y-y0 ) ) // the subtraction converts the coordinates to the palette's local coordinate system
				return j;
		}
		return -1;
	}

	public void draw( GraphicsWrapper gw ) {
		// draw border
		gw.setColor( 0, 0, 0 );
		gw.drawRect( x0, y0, width, height );

		for ( PaletteButton b : buttons ) {
			b.draw( x0, y0, gw );
		}
	}
}
